#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/5/5 8:38
# @Author  : LiShan
# @Email   : lishan_1997@126.com
# @File    : draw.py
# @Note    : this is note
import os
import sys

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

font = {'family' : 'SimSun',
    'weight' : 'bold',
    'size'  : '16'}

path = sys.path[0].replace("\\", "/")

if os.path.exists(path + "/png"):
    pass
else:
    os.mkdir(path + "/png")

# path = sys.path[0]
# up_path = os.getcwd()
# command = "xcopy %s %s /e /k /y /q" % (path+r'\test_best_record.txt', up_path + r'\other\txt\test_dqn_record.txt')
# os.system(command)

# 绘制奖励曲线
file = path + "/train_record.txt"
names = ["delay", "reward", "step", "epsilon", "learn_rate"]
data = pd.read_csv(file, error_bad_lines=False, sep="\s+", names=names)
delay = list(data["delay"].values)
reward = list(data["reward"].values)
step = list(data["step"].values)
epsilon = list(data["epsilon"].values)
learn_rate = list(data["learn_rate"].values)
"""延误"""
# 绘图
x = np.linspace(0, len(delay), len(delay))
plt.plot(x, delay, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([55, 59])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(55, 59.5, 0.5))
# 设置坐标轴名称
# plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
# plt.ylabel("Delay", fontproperties="Times New Roman", size=10.5)
plt.xlabel("迭代回合", fontproperties="SimSun", size=10.5)
plt.ylabel("平均延误", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["delay"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Delay Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/delay.png", dpi=600)
# 关闭绘图
plt.close()

"""奖赏"""
# 绘图
x = np.linspace(0, len(reward), len(reward))
plt.plot(x, reward, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
# plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
# plt.ylabel("Reward", fontproperties="Times New Roman", size=10.5)
plt.xlabel("迭代回合", fontproperties="SimSun", size=10.5)
plt.ylabel("平均奖励", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["reward"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Reward Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/reward.png", dpi=600)
# 关闭绘图
plt.close()

"""回合周期步数（判断是否出现提前结束的回合）"""
# 绘图
x = np.linspace(0, len(step), len(step))
plt.plot(x, step, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
# plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
# plt.ylabel("Reward", fontproperties="Times New Roman", size=10.5)
plt.xlabel("迭代回合", fontproperties="SimSun", size=10.5)
plt.ylabel("训练周期", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["step"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Reward Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/step.png", dpi=600)
# 关闭绘图
plt.close()

"""epsilon"""
# 绘图
x = np.linspace(0, len(epsilon), len(epsilon))
plt.plot(x, epsilon, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
# plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
# plt.ylabel("Epsilon", fontproperties="Times New Roman", size=10.5)
plt.xlabel("迭代回合", fontproperties="SimSun", size=10.5)
plt.ylabel("探索率", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["epsilon"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Epsilon Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/epsilon.png", dpi=600)
# 关闭绘图
plt.close()

"""learn_rate"""
# 绘图
x = np.linspace(0, len(learn_rate), len(learn_rate))
plt.plot(x, learn_rate, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
# plt.xlabel("Episode", fontproperties="Times New Roman", size=10.5)
# plt.ylabel("learn_rate", fontproperties="Times New Roman", size=10.5)
plt.xlabel("迭代回合", fontproperties="SimSun", size=10.5)
plt.ylabel("学习率", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["learn rate"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The learn rate Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/learn_rate.png", dpi=600)
# 关闭绘图
plt.close()

"""损失函数图"""
file = path + "/loss.txt"
with open(file, 'r') as f:
    content = f.readlines()

data = []
begin = 0
for i in range(len(content)):
    line = content[i].split(',')[:-1]
    if not line:
        begin = i + 1
    else:
        line = list(map(float, line))
        average = sum(line)/len(line)
        data.append(average)
# 绘图
x = np.linspace(begin, len(data) + begin, len(data))
plt.plot(x, data, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
plt.ylim([0, 10])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
plt.yticks(np.arange(0, 10.5, 0.5))
# 设置坐标轴名称
plt.xlabel("迭代回合", fontproperties="SimSun", size=10.5)
plt.ylabel("平均误差", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["loss"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Loss Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/loss.png", dpi=600)
# 关闭绘图
plt.close()


"""最优奖赏图"""
file = path + "/best_reward.txt"
with open(file, 'r') as f:
    content = f.readlines()

names = ["episode", "delay", "reward"]
data = pd.read_csv(file, error_bad_lines=False, sep="\t+", names=names, engine="python")
episode = list(data["episode"].values)
best_reward = list(data["reward"].values)

x = episode
data = best_reward
# 绘图
plt.step(x, data, where='post', color='k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
plt.xlabel("迭代回合", fontproperties="SimSun", size=10.5)
plt.ylabel("最优奖励", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["best reward"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Loss Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/best_reward.png", dpi=600)
# 关闭绘图
plt.close()

"""测试奖赏图"""
file = path + "/test_reward.txt"
names = ["episode", "delay", "reward"]
data = pd.read_csv(file, error_bad_lines=False, sep="\t+", names=names, engine="python")
episode = list(data["episode"].values)
test_reward = list(data["reward"].values)

x = episode
data = test_reward
# 绘图
plt.plot(x, data, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([0.3, 1.0])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(0.3, 1.1, 0.1))
# 设置坐标轴名称
plt.xlabel("测试回合", fontproperties="SimSun", size=10.5)
plt.ylabel("历史奖励", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["test reward"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Loss Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/test_reward.png", dpi=600)
# 关闭绘图
plt.close()


"""测试延误图"""
file = path + "/test_reward.txt"
names = ["episode", "delay", "reward"]
data = pd.read_csv(file, error_bad_lines=False, sep="\t+", names=names, engine="python")
episode = list(data["episode"].values)
test_delay = list(data["delay"].values)

x = episode
data = test_delay
# 绘图
plt.plot(x, data, 'k')
# 设置坐标轴范围
# plt.xlim([0, 100])
# plt.ylim([55, 60])
# 设置坐标轴刻度
# plt.xticks(range(0, 110, 10))
# plt.yticks(np.arange(55, 60.5, 0.5))
# 设置坐标轴名称
plt.xlabel("测试回合", fontproperties="SimSun", size=10.5)
plt.ylabel("历史延误", fontproperties="SimSun", size=10.5)
# 设置网格
# plt.grid()
# 设置图例
legend = ["test delay"]
plt.legend(legend, loc="best", frameon=False)
# 设置标题
# plt.title("The Loss Curve", fontproperties="Times New Roman", size=10.5)
# 保存图片
plt.savefig(path + "/png/test_delay.png", dpi=600)
# 关闭绘图
plt.close()

